home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9219 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  110 lines

  1. Path: muss.CIS.McMaster.CA!not-for-mail
  2. From: u9010255@muss.cis.McMaster.CA (S. Jon)
  3. Newsgroups: comp.lang.c
  4. Subject: suspicious pointer conversion warning
  5. Date: 8 Mar 1996 11:16:29 -0500
  6. Organization: McMaster University, Hamilton, Ontario, Canada.
  7. Message-ID: <4hpmgt$4uu@muss.CIS.McMaster.CA>
  8. NNTP-Posting-Host: muss.cis.mcmaster.ca
  9.  
  10. to all the pointer gods out there:
  11.  
  12. i cannot figure out what's wrong with my program.  i always get a 
  13. suspicious pointer conversion warning when i try to reallocate more memory.
  14.  
  15. the purpose of my program is to open a text file and put all the words 
  16. into an array.  i initially set my array for 20 words and if there is 
  17. more than 20 words, i increase the array with a growth factor of 10 by 
  18. using realloc.
  19.  
  20. i would appreciate any input from anyone.  pointers and i just don't 
  21. mix!  i can't wait 'til i try linked lists!!!! :)
  22.  
  23. my program is divided into 3 files: wcount.h, wcount.c (main source file), 
  24. wcount2.c (all my functions).  compiler is borland c++ v3.1 for dos. here is 
  25. selected parts of my program that i think are relevant:
  26.  
  27. --------------------------------------------
  28. //    FILENAME: wcount.h
  29.  
  30. #ifndef WCOUNT_H
  31. #define WCOUNT_H
  32.  
  33. typedef struct
  34. {
  35.   int    iFrequency;
  36.   char    szWord[1];   // variable length array
  37. }  WordInfo;
  38.  
  39. //  FUNCTION PROTOTYPES
  40.  
  41. int Parse (FILE *fpFile, WordInfo *apWords[]);
  42. WordInfo *GetMoreMem (const WordInfo *apWords[], int iWordLimit);
  43.  
  44. //  more function prototypes
  45.  
  46. #endif
  47.  
  48. -----------------------------------------
  49.  
  50. //  FILENAME wcount.c
  51.  
  52. int main ()
  53. {
  54.   int       iWordCount;
  55.   WordInfo  *apWords[20];  // array of pointers to structure WordInfo
  56.   FILE      *fpFile;
  57.  
  58. //  program opens file
  59.  
  60.   iWordCount = Parse (fpFile, apWords);
  61.  
  62. //  program displays the words
  63.  
  64.   return (0);
  65. }
  66.  
  67. ----------------------------------------
  68.  
  69. //  FILENAME wcount2.c
  70.  
  71. int Parse (FILE *fpFile, WordInfo *apWords[])
  72. {
  73.   int    iWordLimit = 20;  // initially limited to 20 words
  74.   int    iWordCount;   // running count on the number of words in array
  75.  
  76. //    program parses words here and puts word in array with malloc
  77. //    program also compares any new word with words in array to avoid 
  78. //    duplication
  79.  
  80.   if (iWordCount == iWordLimit)
  81.   {
  82.     iWordLimit += 10;
  83.  
  84. //  ***** SUSPICIOUS POINTER ERROR REFERS TO LINE BELOW
  85.  
  86.     apWords = GetMoreMem (apWords, iWordLimit);
  87.  
  88.   }
  89.  
  90.   return (iWordCount);
  91. }
  92.  
  93. WordInfo *GetMoreMem (const WordInfo *apWords[], int iWordLimit)
  94. {
  95.   WordInfo *pTemp;    // temp pointer to struct WordInfo
  96.  
  97.   pTemp = (WordInfo *)realloc(apWords, iWordLimit*sizeof(WordInfo));
  98.  
  99.   if (pTemp == NULL)
  100.   {
  101.     exit (EXIT_FAILURE);
  102.   }
  103.   return (pTemp);
  104. }
  105. --------------------------------------------
  106.  
  107. thanks for reading it.  :)
  108.  
  109. s
  110.